package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.CodeRuleDTO;
import com.sanshi.project.admin.api.entity.CodeRuleEntity;
import com.sanshi.project.assets.service.CodeRuleService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编码规则配置表
 *
 * @author nikon
 * @date 2023-11-10 19:51:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/codeRule")
@Tag(description = "codeRule", name = "编码规则配置表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CodeRuleController {

    private final CodeRuleService codeRuleService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param codeRule 编码规则配置表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_codeRule_view')")
    public R getCodeRulePage(@ParameterObject Page page, @ParameterObject CodeRuleDTO codeRule) {
        LambdaQueryWrapper<CodeRuleEntity> wrapper = Wrappers.lambdaQuery();
        // 业务类型
        wrapper.in(ArrayUtil.isNotEmpty(codeRule.getType()), CodeRuleEntity::getType, codeRule.getType());
        // 名称
        wrapper.like(StrUtil.isNotBlank(codeRule.getName()), CodeRuleEntity::getName, codeRule.getName());
        // 状态
        wrapper.in(ArrayUtil.isNotEmpty(codeRule.getStatus()), CodeRuleEntity::getStatus, codeRule.getStatus());
        // 倒序查询
        wrapper.orderByDesc(CodeRuleEntity::getUpdateTime);
        return R.ok(codeRuleService.page(page, wrapper));
    }

    /**
     * 通过id查询编码规则配置表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_codeRule_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(codeRuleService.getByIdDeep(id));
    }

    /**
     * 新增编码规则配置表
     *
     * @param codeRule 编码规则配置表
     * @return R
     */
    @Operation(summary = "新增编码规则配置表", description = "新增编码规则配置表")
    @SysLog("新增编码规则配置表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_codeRule_add')")
    public R save(@RequestBody CodeRuleEntity codeRule) {
        // 查询某业务类型是否已经存在并开启
        List<CodeRuleEntity> codeRuleList = codeRuleService.list(Wrappers.<CodeRuleEntity>query().lambda().eq(CodeRuleEntity::getType, codeRule.getType()).eq(CodeRuleEntity::getStatus, "1"));
        if (CollUtil.isNotEmpty(codeRuleList)) {
            return R.failed("该业务类型编码已存在，请先停用之前的类型编码编码规则！");
        }
        return R.ok(codeRuleService.saveDeep(codeRule));
    }

    /**
     * 修改编码规则配置表
     *
     * @param codeRule 编码规则配置表
     * @return R
     */
    @Operation(summary = "修改编码规则配置表", description = "修改编码规则配置表")
    @SysLog("修改编码规则配置表")
    @PutMapping("/updateById")
    @PreAuthorize("@pms.hasPermission('assets_codeRule_edit')")
    public R updateById(@RequestBody CodeRuleEntity codeRule) {
        // 查询某业务类型是否已经存在并开启
        List<CodeRuleEntity> codeRuleList = codeRuleService.list(Wrappers.<CodeRuleEntity>query().lambda().eq(CodeRuleEntity::getType, codeRule.getType()).eq(CodeRuleEntity::getStatus, "1").ne(CodeRuleEntity::getId, codeRule.getId()));
        if (CollUtil.isNotEmpty(codeRuleList)) {
            return R.failed("该业务类型编码已存在，请先停用之前的类型编码编码规则！");
        }
        return R.ok(codeRuleService.updateDeep(codeRule));
    }

    /**
     * 启动或者关闭编码规则
     *
     * @param codeRule 编码规则配置表
     * @return R
     */
    @Operation(summary = "启动或者关闭编码规则", description = "启动或者关闭编码规则")
    @SysLog("启动或者关闭编码规则")
    @PutMapping("/updateStatus")
    @PreAuthorize("@pms.hasPermission('assets_codeRule_edit')")
    public R updateStatus(@RequestBody CodeRuleEntity codeRule) {
        return R.ok(codeRuleService.updateById(codeRule));
    }

    /**
     * 通过id删除编码规则配置表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除编码规则配置表", description = "通过id删除编码规则配置表")
    @SysLog("通过id删除编码规则配置表")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_codeRule_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(codeRuleService.removeDeep(ids));
    }

    /**
     * 通过id删除编码规则配置表子表数据
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除编码规则配置表子表数据", description = "通过id删除编码规则配置表子表数据")
    @SysLog("通过id删除编码规则配置表子表数据")
    @DeleteMapping("/child")
    @PreAuthorize("@pms.hasPermission('assets_codeRule_del')")
    public R removeChild(@RequestBody Long[] ids) {
        return R.ok(codeRuleService.removeChild(ids));
    }

    /**
     * 导出excel 表格
     *
     * @param codeRule 查询条件
     * @param ids      导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_codeRule_export')")
    public List<CodeRuleEntity> export(CodeRuleEntity codeRule, Long[] ids) {
        return codeRuleService.list(Wrappers.lambdaQuery(codeRule).in(ArrayUtil.isNotEmpty(ids), CodeRuleEntity::getId, ids));
    }
}